﻿package mortal.game.scene3D.model.player
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import com.gengine.debug.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.core.mesh.*;
    import frEngine.event.*;
    import frEngine.loaders.away3dMd5.*;
    import frEngine.manager.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.fragmentFilters.*;
    import mortal.common.tools.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;

    public class ActionPlayer extends Md5Mesh implements IGame2D, IHang
    {
        private var _y2d:Number = 0;
        private var _x2d:Number = 0;
        private var _direction:Number = 90;
        private var _hangBoneName:String;
        private var _selectEnabled:Boolean = false;
        private var _currentActionName:String;
        private var _textTure:String;
        private var _hasDispose:Boolean = false;
        public var enabeldDrawDefault:Boolean = false;
        private var _cutexture:Texture3D;
        private var _phantomEnabled:Boolean = false;
        private var _phantomDrawList:Vector.<JumpPos>;
        private var _temptVector:Vector3D;
        private var _phantomcurPlayTrackName:String;
        private var _tempcurPlayTrackName:String;
        private var _preFrameId:int = -1;
        private var _oldBlendType:int = -1;

        public function ActionPlayer(param1:int = 2)
        {
            this._phantomDrawList = new Vector.<JumpPos>;
            this._temptVector = new Vector3D();
            super("");
            this.loadPriority = param1;
            return;
        }// end function

        public function set scaleValue(param1:Number) : void
        {
            this.scaleX = param1;
            this.scaleY = param1;
            this.scaleZ = param1;
            return;
        }// end function

        public function get scaleValue() : Number
        {
            return this.scaleX;
        }// end function

        public function set selectEnabled(param1:Boolean) : void
        {
            this._selectEnabled = param1;
            return;
        }// end function

        public function get selectEnabled() : Boolean
        {
            return this._selectEnabled;
        }// end function

        public function addFrameComplete(param1:String, param2:Function) : void
        {
            targetMd5Controler.addFrameScript(param1, Modifier.maxFrame, param2);
            return;
        }// end function

        public function removeFrameComplete(param1:String, param2:Function) : void
        {
            targetMd5Controler.removeFrameScript(param1, Modifier.maxFrame, param2);
            return;
        }// end function

        public function addFrameScript(param1:String, param2:int, param3:Function) : void
        {
            targetMd5Controler.addFrameScript(param1, param2, param3);
            return;
        }// end function

        public function removeAllFrameScript() : void
        {
            targetMd5Controler.removeAllFrameScript();
            return;
        }// end function

        public function removeFrameScript(param1:String, param2:int, param3:Function) : void
        {
            targetMd5Controler.removeFrameScript(param1, param2, param3);
            return;
        }// end function

        public function hasAction(param1:String) : Boolean
        {
            var _loc_2:* = targetMd5Controler.labels[param1];
            return _loc_2 != null;
        }// end function

        public function get actionName() : String
        {
            return targetMd5Controler.curPlayTrackName;
        }// end function

        public function get currentFrame() : int
        {
            return targetMd5Controler.currentFrame;
        }// end function

        public function set currentFrame(param1:int) : void
        {
            targetMd5Controler.currentFrame = param1;
            return;
        }// end function

        public function get textTure() : String
        {
            return this._textTure;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            if (this._hasDispose)
            {
                return;
            }
            this._oldBlendType = -1;
            this._hasDispose = true;
            super.dispose(param1);
            this.stop();
            this._y2d = 0;
            this._x2d = 0;
            this.enabeldDrawDefault = false;
            this._direction = 90;
            this._cutexture = null;
            this._hangBoneName = null;
            this._textTure = null;
            this.selectEnabled = false;
            this.scaleValue = 1;
            return;
        }// end function

        public function get hangBody() : Pivot3D
        {
            return this;
        }// end function

        public function hang(param1:IHang) : void
        {
            if (param1.hangBoneName == null)
            {
                this.addChild(param1.hangBody);
            }
            else if (!this.hasHang(param1))
            {
                targetMd5Controler.attachObjectToBone(param1.hangBoneName, param1.hangBody);
            }
            return;
        }// end function

        public function hasBone(param1:String) : Boolean
        {
            return targetMd5Controler.getJointIndexFromName(param1) >= 0;
        }// end function

        public function unHang(param1:IHang) : void
        {
            targetMd5Controler.removeHang(param1.hangBody);
            return;
        }// end function

        public function hasHang(param1:IHang) : Boolean
        {
            return param1.hangBody.curHangControler == targetMd5Controler;
        }// end function

        public function get hangBoneName() : String
        {
            return this._hangBoneName;
        }// end function

        public function set hangBoneName(param1:String) : void
        {
            this._hangBoneName = param1;
            return;
        }// end function

        public function set x2d(param1:Number) : void
        {
            this._x2d = param1;
            x = Scene3DUtil.change2Dto3DX(param1);
            return;
        }// end function

        public function get x2d() : Number
        {
            return this._x2d;
        }// end function

        public function set y2d(param1:Number) : void
        {
            this._y2d = param1;
            z = Scene3DUtil.change2Dto3DY(param1);
            return;
        }// end function

        public function get y2d() : Number
        {
            return this._y2d;
        }// end function

        public function load(param1:String, param2:String, param3:String, param4:RenderList = null, param5:Number = 0.5, param6:int = 0) : void
        {
            var _loc_7:Boolean = false;
            if (param4 == null)
            {
                param4 = Global3D.scene.renderLayerList;
            }
            this._hasDispose = false;
            this.renderList = param4;
            this.clear();
            if (!Global3D.loadResource)
            {
                return;
            }
            this.meshUrl = param1;
            this.initAnimate(param2);
            if (this._textTure != param3)
            {
                this._cutexture = null;
                _loc_7 = Resource3dKilAlpha.instance.isJpg(param3);
                if (param6 == EBlendType.BLEND_NONE)
                {
                    param6 = BlendTypeConfig.instance.getBlendType(param3);
                }
                this.setBlendModeAndKillAlpha(!_loc_7, param5, param6);
                this._textTure = param3;
                this.setMaterial(param3, Texture3D.MIP_NONE, param3);
            }
            return;
        }// end function

        public function set direction(param1:Number) : void
        {
            if (this._direction == param1)
            {
                return;
            }
            this._direction = param1;
            var _loc_2:* = Scene3DUtil.change2Dto3DRotation(this._direction);
            this.rotationY = _loc_2;
            return;
        }// end function

        public function get direction() : Number
        {
            return this._direction;
        }// end function

        public function changeAction(param1:String) : void
        {
            if (DebugConst.isOutPutChangeAction)
            {
                Log.debug("切换动作：", param1, this.meshUrl);
            }
            if (targetMd5Controler.curPlayTrackName != param1)
            {
                targetMd5Controler.setPlayLable(param1);
                this.gotoFrame(0, param1);
            }
            return;
        }// end function

        public function getAttackFrame(param1:String) : uint
        {
            var _loc_2:* = targetMd5Controler.getLabel(param1);
            if (!_loc_2)
            {
                return 0;
            }
            return _loc_2.fightOnFrame;
        }// end function

        public function gotoFrame(param1:int, param2:String = null) : void
        {
            if (targetMd5Controler.isPlaying)
            {
                targetMd5Controler.gotoAndPlay(param1, param2);
            }
            else
            {
                targetMd5Controler.gotoAndStop(param1, param2);
            }
            return;
        }// end function

        public function play(param1:int = 0) : void
        {
            if (!targetMd5Controler.isPlaying)
            {
                targetMd5Controler.play();
            }
            this.animationMode = param1;
            this.timerContorler.active();
            return;
        }// end function

        public function stop() : void
        {
            if (targetMd5Controler.isPlaying)
            {
                targetMd5Controler.stop();
            }
            this.timerContorler.unActive();
            return;
        }// end function

        override protected function setTextureFilter(param1, param2:int) : FragmentFilter
        {
            this._cutexture = Resource3dManager.instance.getTexture3d(param1, param2, this.loadPriority);
            param1 = new TextureFilter(this._cutexture);
            return param1;
        }// end function

        override public function draw(param1:Boolean = true, param2:ShaderBase = null) : void
        {
            var _loc_3:int = 0;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:JumpPos = null;
            var _loc_10:JumpPos = null;
            if (!this._cutexture)
            {
                if (param1)
                {
                    _loc_3 = children.length - 1;
                    while (_loc_3 >= 0)
                    {
                        
                        children[_loc_3].draw(true, param2);
                        _loc_3 = _loc_3 - 1;
                    }
                }
                return;
            }
            if (!_scene)
            {
                this.upload(Global3D.scene);
            }
            this.targetMd5Controler.enabledDraw = this.initMeshCompleted;
            if (this.visible && this.targetMd5Controler.enabledDraw)
            {
                this._tempcurPlayTrackName = this.targetMd5Controler.curPlayTrackName;
                render.draw(this, param2);
                if (this.PhantomEnabled || !this.PhantomEnabled && this._phantomDrawList.length > 0)
                {
                    if (this._phantomDrawList.length > 0)
                    {
                        this.getPosition(false, this._temptVector);
                        _loc_4 = this.alpha;
                        _loc_5 = this.rotationY;
                        _loc_6 = this.currentFrame;
                        _loc_7 = this._phantomDrawList.length;
                        _loc_8 = 0;
                        while (_loc_8 < _loc_7)
                        {
                            
                            _loc_9 = this._phantomDrawList[_loc_8];
                            _loc_9.alpha = _loc_9.alpha - 0.05;
                            this.world.copyColumnFrom(3, _loc_9);
                            this.alpha = _loc_9.alpha;
                            this.rotationY = _loc_9.rotationY;
                            this.targetMd5Controler.setCurActionFrame(this._phantomcurPlayTrackName, _loc_9.currentFrame);
                            render.draw(this, param2);
                            _loc_8++;
                        }
                        if (this.alpha < 0)
                        {
                            this._phantomDrawList.shift();
                        }
                        this.alpha = _loc_4;
                        this.world.copyColumnFrom(3, this._temptVector);
                        this.rotationY = _loc_5;
                        this.targetMd5Controler.setCurActionFrame(this._tempcurPlayTrackName, _loc_6);
                    }
                    if (this.PhantomEnabled)
                    {
                        if (this._preFrameId != this.currentFrame && this.currentFrame % 2 == 0)
                        {
                            _loc_10 = new JumpPos();
                            _loc_10.x = this._temptVector.x;
                            _loc_10.y = this._temptVector.y;
                            _loc_10.z = this._temptVector.z;
                            _loc_10.rotationY = this.rotationY;
                            _loc_10.currentFrame = this.currentFrame;
                            _loc_10.alpha = 1;
                            this._phantomDrawList.push(_loc_10);
                            this._preFrameId = this.currentFrame;
                            if (this._phantomDrawList.length > 5)
                            {
                                this._phantomDrawList.shift();
                            }
                        }
                    }
                }
            }
            else if (this.enabeldDrawDefault)
            {
                this.world.copyColumnTo(3, this._temptVector);
                DefaultPlayer.instance.drawImp(this._temptVector);
            }
            if (param1)
            {
                _loc_3 = children.length - 1;
                while (_loc_3 >= 0)
                {
                    
                    children[_loc_3].draw(true, param2);
                    _loc_3 = _loc_3 - 1;
                }
            }
            if (_eventFlags & EXIT_DRAW_FLAG)
            {
                FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.EXIT_DRAW_EVENT);
            }
            return;
        }// end function

        public function get PhantomEnabled() : Boolean
        {
            return this._phantomEnabled;
        }// end function

        public function set PhantomEnabled(param1:Boolean) : void
        {
            this._phantomEnabled = param1;
            if (param1)
            {
                this._phantomcurPlayTrackName = this.targetMd5Controler.curPlayTrackName;
            }
            return;
        }// end function

        public function openUnlevelAlpha(param1:Number = 0.4) : void
        {
            this.material.materialParams.unlevelAlpha = param1;
            if (param1 < 1)
            {
                this._oldBlendType = this.material.materialParams.blendMode;
                this.setMateiralBlendMode(EBlendType.BLEND_ALPHA3);
            }
            else
            {
                if (this._oldBlendType != -1)
                {
                    this.setMateiralBlendMode(this._oldBlendType);
                }
                this._oldBlendType = -1;
            }
            return;
        }// end function

        public function getBoneGlobleY(param1:String, param2:ActionPlayer = null, param3:String = null, param4:String = null) : Number
        {
            var _loc_6:Vector3D = null;
            var _loc_7:SkeletonClipNode = null;
            var _loc_8:SkeletonPose = null;
            var _loc_5:Number = 150;
            if (this.hasBone(param1))
            {
                _loc_6 = this.targetMd5Controler.getGJointPoseByName(param1);
                if (_loc_6)
                {
                    _loc_5 = _loc_6.y * this.scaleY;
                    if (param2 && param2.hasBone(param4))
                    {
                        _loc_7 = param2.targetMd5Controler.getLabel(param3) as SkeletonClipNode;
                        if (_loc_7 && _loc_7.hasLoaded)
                        {
                            _loc_8 = _loc_7.getGloblePoses(0);
                            _loc_6 = param2.targetMd5Controler.getGJointPoseByName(param4);
                            var _loc_9:* = _loc_5 + _loc_6.y * param2.scaleY;
                            _loc_5 = _loc_5 + _loc_6.y * param2.scaleY;
                        }
                    }
                    if (this.scaleY > 1)
                    {
                        _loc_5 = _loc_5 - 30 * this.scaleY;
                    }
                }
            }
            return _loc_5;
        }// end function

        public function getActionFrameLen(param1:String) : int
        {
            var _loc_2:* = targetMd5Controler.labels[param1];
            if (_loc_2)
            {
                return _loc_2.length;
            }
            return 1;
        }// end function

    }
}
